package router

import (
	"MyBlogv2/common/middlewares"
	service "MyBlogv2/service"

	"github.com/gin-gonic/gin"
)

func Router() *gin.Engine {
	// 设置gin模式
	gin.SetMode(gin.ReleaseMode)
	r := gin.Default()
	r.Use(middlewares.CORSMiddleware())
	r.GET("/banners/list", service.GetBanners)

	public := r.Group("/public")
	{
		public.GET("/ask-code", service.SendCodeOfRegister)
	}
	banners := r.Group("/banners")
	{
		banners.POST("/upload/banner", middlewares.AuthPermission("blog:banner:add"), service.UploadBanner)
		banners.PUT("/update/sort/order", middlewares.AuthPermission("blog:banner:update"), service.UpdateSortOrder)
		banners.DELETE("/:id", middlewares.AuthPermission("blog:banner:delete"), service.DelBannerById)
		banners.GET("/back/list", middlewares.AuthPermission("blog:banner:list"), service.GetBanners4Admin)
	}
	user := r.Group("/user")
	{
		user.POST("/register", service.Register)
		user.POST("/login", service.Login)
		user.POST("/logout", service.Logout)
		user.GET("/auth/info", middlewares.AuthUserCheck(), service.GetUserInfo)
		user.POST("/auth/upload/avatar", middlewares.AuthUserCheck(), service.UpdateAvatar)
		user.POST("/auth/update", middlewares.AuthUserCheck(), service.UpdateUserInfo)
		user.GET("/list", middlewares.AuthPermission("system:user:list"), service.GetUserList)
		user.POST("/search", middlewares.AuthPermission("system:user:search"), service.SearchUserList)
		user.GET("/details/:id", middlewares.AuthPermission("system:user:details"), service.FindUserDetailById)
		user.DELETE("/delete", middlewares.AuthPermission("system:user:delete"), service.DeleteUserByIds)
		user.POST("/update/status", middlewares.AuthPermission("system:user:status:update"), service.UpdateUserStatus)
	}
	article := r.Group("/article")
	{
		article.GET("/recommend", service.GetRecommendList)
		article.GET("/list", service.GetArticlePageList)
		article.GET("/hot", service.GetHotArticleList)
		article.GET("/where/list/:typeId", service.GetArticleListByTypeId)
		article.GET("/detail/:id", service.GetArticleDetailById)
		article.GET("/visit/:id", service.UpdateVisitCount)
		article.GET("/related/:categoryId/:articleId", service.Related)
		article.GET("/random", service.GetRandomArticleList)
		article.GET("/search/init/title", service.InitSearchByTitle)
		article.GET("/search/by/content", service.SearchArticleByContent)
		article.GET("/timeLine", service.GetTimeLineList)
		article.POST("/upload/articleCover", middlewares.AuthPermission("blog:publish:article"), service.UploadArticleCover)
		article.POST("/publish", middlewares.AuthPermission("blog:publish:article"), service.PublishArticle)
		article.POST("/upload/articleImage", middlewares.AuthPermission("blog:publish:article"), service.UploadArticleImage)
		article.POST("/delete/articleCover", middlewares.AuthPermission("blog:publish:article"), service.DeleteArticleCover)
		article.GET("/back/list", middlewares.AuthPermission("blog:article:list"), service.GetArticleList)
		article.POST("/back/search", middlewares.AuthPermission("blog:article:search"), service.SearchArticle)
		article.POST("/back/update/status", middlewares.AuthPermission("blog:article:update"), service.UpdateArticleStatus)
		article.POST("/back/update/isTop", middlewares.AuthPermission("blog:article:update"), service.UpdateArticleIsTop)
		article.GET("/back/echo/:id", middlewares.AuthPermission("blog:article:echo"), service.GetArticleEcho)
		article.DELETE("/back/delete", middlewares.AuthPermission("blog:article:delete"), service.DeleteArticle)

	}
	comment := r.Group("/comment")
	{
		comment.POST("/auth/add/comment", middlewares.AuthUserCheck(), service.AddComment)
		comment.GET("/getComment", service.GetCommentList)
		comment.POST("/back/search", middlewares.AuthPermission("blog:comment:search"), service.SearchComment)
		comment.POST("/back/isCheck", middlewares.AuthPermission("blog:comment:isCheck"), service.UpdateCommentIsCheck)
		comment.GET("/back/list", middlewares.AuthPermission("blog:comment:list"), service.GetCommentList4Admin)
		comment.DELETE("/back/delete/:id", middlewares.AuthPermission("blog:comment:delete"), service.DeleteCommentById)
	}
	websiteInfo := r.Group("/websiteInfo")
	{
		websiteInfo.GET("/front", service.GetWebsiteInfo)
		websiteInfo.GET("", middlewares.AuthPermission("blog:get:websiteInfo"), service.GetWebsiteInfo)
		websiteInfo.POST("/upload/avatar", middlewares.AuthPermission("blog:update:websiteInfo"), service.UploadAvatar)
		websiteInfo.POST("/upload/background", middlewares.AuthPermission("blog:update:websiteInfo"), service.UploadBackground)
		websiteInfo.POST("/stationmaster", middlewares.AuthPermission("blog:update:websiteInfo"), service.UpdateStationmasterInfo)
		websiteInfo.POST("/webInfo", middlewares.AuthPermission("blog:update:websiteInfo"), service.UpdateWebsiteInfo)

	}
	like := r.Group("/like")
	{
		like.POST("/auth/like", middlewares.AuthUserCheck(), service.Like)
		like.DELETE("/auth/like", middlewares.AuthUserCheck(), service.CancelLike)
		like.GET("/whether/like", middlewares.AuthUserCheck(), service.IsLike)
	}
	link := r.Group("/link")
	{
		link.GET("/list", service.GetLinkList)
		link.POST("/auth/apply", middlewares.AuthUserCheck(), service.ApplyLink)
		link.GET("/back/list", middlewares.AuthPermission("blog:link:list"), service.GetBackLinkList)
		link.POST("back/search", middlewares.AuthPermission("blog:link:search"), service.SearchLink)
		link.POST("/back/isCheck", middlewares.AuthPermission("blog:link:isCheck"), service.UpdateLinkIsCheck)
		link.DELETE("/back/delete", middlewares.AuthPermission("blog:link:delete"), service.DeleteLink)
	}
	category := r.Group("/category")
	{
		category.GET("/list", service.GetCategoryList)
		category.POST("/back/search", middlewares.AuthPermission("blog:category:search"), service.SearchCategory)
		category.POST("/back/update", middlewares.AuthPermission("blog:category:update"), service.AddOrUpdateCategory)
		category.DELETE("/back/delete", middlewares.AuthPermission("blog:category:delete"), service.DeleteCategory)
		category.POST("/back/add", middlewares.AuthPermission("blog:category:add"), service.AddCategory)
		category.GET("/back/list", middlewares.AuthPermission("blog:category:list"), service.GetCategoryList4Admin)
		category.GET("/back/get/:id", middlewares.AuthPermission("blog:category:search"), service.GetCategoryById)
		category.PUT("", middlewares.AuthPermission("blog:category:add"), service.AddCategory4Category)
	}
	favorite := r.Group("/favorite")
	{
		favorite.POST("/auth/favorite", middlewares.AuthUserCheck(), service.Favorite)
		favorite.DELETE("/auth/favorite", middlewares.AuthUserCheck(), service.CancelFavorite)
		favorite.GET("/whether/favorite", middlewares.AuthUserCheck(), service.IsFavorite)
		favorite.GET("/back/list", middlewares.AuthPermission("blog:favorite:list"), service.GetFavoriteList4Admin)
		favorite.POST("/back/search", middlewares.AuthPermission("blog:favorite:search"), service.SearchFavorite)
		favorite.POST("/back/isCheck", middlewares.AuthPermission("blog:favorite:isCheck"), service.UpdateFavoriteIsCheck)
		favorite.DELETE("/back/delete", middlewares.AuthPermission("blog:favorite:delete"), service.DeleteFavorite)
	}
	leaveWord := r.Group("/leaveWord")
	{
		leaveWord.GET("/list", middlewares.AuthUserCheck(), service.GetLeaveWordList)
		leaveWord.POST("/auth/userLeaveWord", middlewares.AuthUserCheck(), service.AddLeaveWord)
		leaveWord.GET("/back/list", middlewares.AuthPermission("blog:leaveword:list"), service.GetLeaveWordList4Admin)
		leaveWord.POST("/back/search", middlewares.AuthPermission("blog:leaveword:search"), service.SearchLeaveWord)
		leaveWord.DELETE("/back/delete", middlewares.AuthPermission("blog:leaveword:delete"), service.DeleteLeaveWord)
		leaveWord.POST("/back/isCheck", middlewares.AuthPermission("blog:leaveword:isCheck"), service.UpdateLeaveWordIsCheck)
	}
	tag := r.Group("/tag")
	{
		tag.GET("/list", service.GetTagList)
		tag.GET("/back/list", middlewares.AuthPermission("blog:tag:list"), service.GetArticleTagList)
		tag.POST("/back/search", middlewares.AuthPermission("blog:tag:search"), service.SearchTag)
		tag.GET("/back/get/:id", middlewares.AuthPermission("blog:tag:search"), service.GetTagById)
		tag.PUT("/back/add", middlewares.AuthPermission("blog:tag:add"), service.AddOrUpdateTag)
		tag.DELETE("/back/delete", middlewares.AuthPermission("blog:tag:delete"), service.DeleteTagByIds)
		tag.POST("/back/update", middlewares.AuthPermission("blog:tag:update"), service.AddOrUpdateTag)
		tag.PUT("", middlewares.AuthPermission("blog:tag:add"), service.AddTag4Article)

	}
	treeHole := r.Group("/treeHole")
	{
		treeHole.POST("/auth/addTreeHole", middlewares.AuthUserCheck(), service.AddTreeHole)
		treeHole.GET("/getTreeHoleList", service.GetTreeHoleList)
		treeHole.POST("/back/search", middlewares.AuthPermission("blog:treeHole:search"), service.SearchTreeHole)
		treeHole.POST("/back/isCheck", middlewares.AuthPermission("blog:treeHole:isCheck"), service.UpdateTreeHoleIsCheck)
		treeHole.GET("/back/list", middlewares.AuthPermission("blog:treeHole:list"), service.GetTreeHoleList4Admin)
		treeHole.DELETE("/back/delete", middlewares.AuthPermission("blog:treeHole:delete"), service.DeleteTreeHole)
	}
	menu := r.Group("/menu")
	{
		menu.GET("/router/list/:typeId", service.GetMenuList)
		menu.GET("/list/:typeId", middlewares.AuthPermission("system:menu:list"), service.GetMenuListByTypeId)
	}
	role := r.Group("/role")
	{
		role.GET("/list", middlewares.AuthPermission("system:role:list"), service.GetRoleList)
		role.POST("/update/status", middlewares.AuthPermission("system:role:status:update"), service.UpdateRoleStatus)
		role.PUT("/update", middlewares.AuthPermission("system:role:update"), service.UpdateRole)
		role.GET("/get/:id", middlewares.AuthPermission("system:role:get"), service.GetRoleById)
		role.POST("/search", middlewares.AuthPermission("system:role:search"), service.SearchRole)
		role.PUT("/add", middlewares.AuthPermission("system:role:add"), service.AddRole)
		role.DELETE("/delete", middlewares.AuthPermission("system:role:delete"), service.DeleteRoleByIds)
	}
	permission := r.Group("/permission")
	{
		permission.GET("/list", middlewares.AuthPermission("system:permission:list"), service.GetPermissionList)
		permission.GET("/search", middlewares.AuthPermission("system:permission:search"), service.SearchPermission)
		permission.GET("/menu", middlewares.AuthPermission("system:permission:menu:list"), service.GetPermissionMenuList)
		permission.POST("/add", middlewares.AuthPermission("system:permission:add"), service.AddOrUpdatePermission)
		permission.DELETE("/delete/:id", middlewares.AuthPermission("system:permission:delete"), service.DeletePermission)
		permission.GET("/get/:id", middlewares.AuthPermission("system:permission:get"), service.GetPermission)
		permission.POST("/update", middlewares.AuthPermission("system:permission:update"), service.AddOrUpdatePermission)

	}
	rolePermission := r.Group("/role/permission")
	{
		rolePermission.GET("/role/list", middlewares.AuthPermission("system:permission:role:list"), service.GetRoleHasPermission)
		rolePermission.GET("/not/role/list", middlewares.AuthPermission("system:permission:role:not:list"), service.GetRoleNoPermission)
		rolePermission.POST("/add", middlewares.AuthPermission("system:permission:role:add"), service.AddRolePermission)
		rolePermission.DELETE("/delete", middlewares.AuthPermission("system:permission:role:delete"), service.DeleteRolePermission)
	}
	userRole := r.Group("/user/role")
	{
		userRole.GET("/user/list", middlewares.AuthPermission("system:user:role:list"), service.GetUserHasRole)
		userRole.GET("/not/user/list", middlewares.AuthPermission("system:not:role:user:list"), service.GetUserNoRole)
		userRole.POST("/add", middlewares.AuthPermission("system:user:role:add"), service.AddUserRole)
		userRole.DELETE("/delete", middlewares.AuthPermission("system:user:role:delete"), service.DeleteUserRole)
		userRole.GET("/role/list", middlewares.AuthPermission("system:role:user:list"), service.GetRoleHasUser)
		userRole.GET("/not/role/list", middlewares.AuthPermission("system:user:role:not:list"), service.GetRoleNoUser)
	}
	// API路由 - AI聊天相关接口（需要登录）
	aiChat := r.Group("")
	{
		// 会话管理
		aiChat.GET("/sessions",  middlewares.AuthUserCheck(), service.GetSessions)          // 获取所有会话
		aiChat.POST("/sessions", middlewares.AuthUserCheck(), service.CreateSession)       // 创建新会话
		aiChat.GET("/sessions/:id", middlewares.AuthUserCheck(), service.LoadSession)      // 加载指定会话
		aiChat.DELETE("/sessions/:id", middlewares.AuthUserCheck(), service.DeleteSession) // 删除指定会话
		aiChat.POST("/chat", middlewares.AuthUserCheck(), service.ChatStream)              // AI Chat聊天接口（真正流式）
		aiChat.GET("/models", service.GetAllContentModel)     // 获取所有内容模型
	}

	agentChat := r.Group("")
	{
		agentChat.GET("/agent/models", service.GetAllAgentModel)                                          // 获取所有Agent模型
		agentChat.POST("/agent/chat", middlewares.AuthUserCheck(), service.AgentChat)                     // AI Agent聊天接口（支持工具调用）
		agentChat.GET("/agent/sessions", middlewares.AuthUserCheck(), service.GetAgentSessions)           // 获取所有会话
		agentChat.POST("/agent/sessions", middlewares.AuthUserCheck(), service.CreateAgentSession)        // 创建新会话
		agentChat.GET("/agent/sessions/:id", middlewares.AuthUserCheck(), service.LoadAgentSession)       // 加载指定会话
		agentChat.DELETE("/agent/sessions/:id", middlewares.AuthUserCheck(), service.DeleteAgentSession)  // 删除指定会话
		agentChat.GET("/agent/article/:id", middlewares.AuthUserCheck(), service.GetArticle)              // 获取指定会话的文章
		agentChat.POST("/agent/save/article", middlewares.AuthUserCheck(), service.SaveArticle)           // 保存指定会话的文章
	}

	backendAgentChat := r.Group("")
	{
		backendAgentChat.POST("/backend/agent/chat", service.BackendAgentChat) // 后台AI Agent聊天接口（支持SQL工具）
		backendAgentChat.GET("/backend/agent/sessions", service.GetBackendAgentSessions) // 获取所有后台会话
		backendAgentChat.POST("/backend/agent/sessions", service.CreateBackendAgentSession) // 创建新后台会话
		backendAgentChat.GET("/backend/agent/sessions/:id", service.LoadBackendAgentSession) // 加载指定后台会话
		backendAgentChat.DELETE("/backend/agent/sessions/:id", service.DeleteBackendAgentSession) // 删除指定后台会话
		backendAgentChat.POST("/backend/agent/confirm-sql", service.ConfirmSQL) // SQL 确认接口
	}

	recommend := r.Group("/recommend")
	{
		// 主页推荐（支持已登录和未登录用户）
		recommend.GET("/home", middlewares.OptionalAuth(), service.GetHomeRecommendArticles)
		
		// 文章详细页推荐相似文章
		recommend.GET("/similar/:articleId", service.GetSimilarArticles)
	}

	neo4jSync := r.Group("/neo4j")
	{
		neo4jSync.POST("/sync/all", middlewares.AuthPermission("system:neo4j:sync"), service.SyncAllDataToNeo4jHandler)
		neo4jSync.POST("/sync/users", middlewares.AuthPermission("system:neo4j:sync"), service.SyncAllUsersToNeo4jHandler)
		neo4jSync.POST("/sync/articles", middlewares.AuthPermission("system:neo4j:sync"), service.SyncAllArticlesToNeo4jHandler)
		neo4jSync.POST("/sync/likes", middlewares.AuthPermission("system:neo4j:sync"), service.SyncAllLikesToNeo4jHandler)
		neo4jSync.DELETE("/clear", middlewares.AuthPermission("system:neo4j:clear"), service.ClearNeo4jDataHandler)
	}

	// 知识图谱可视化接口
	graph := r.Group("/graph")
	{
		// 查询接口（公开访问或需要登录权限）
		graph.GET("/overview", service.GetGraphOverview)                            // 获取图谱概览
		graph.GET("/nodes", service.GetGraphNodes)                                  // 获取节点列表（分页）
		graph.POST("/query", service.ExecuteGraphQuery)                             // 执行自定义Cypher查询
		// graph.GET("/nodes/:id", service.GetGraphNodeDetail)                         // 获取节点详情
		// graph.GET("/nodes/:id/neighbors", service.GetGraphNodeNeighbors)           // 获取节点邻居
		// graph.GET("/search", service.SearchGraphNodes)                              // 搜索节点
		// graph.GET("/stats", service.GetGraphStats)                                  // 统计分析
		
		// 编辑接口（需要权限）
		// graph.POST("/edit/nodes", middlewares.AuthPermission("graph:node:create"), service.CreateGraphNode)
		// graph.PUT("/edit/nodes/:id", middlewares.AuthPermission("graph:node:update"), service.UpdateGraphNode)
		// graph.DELETE("/edit/nodes/:id", middlewares.AuthPermission("graph:node:delete"), service.DeleteGraphNode)
		// graph.POST("/edit/relationships", middlewares.AuthPermission("graph:edge:create"), service.CreateGraphRelationship)
		// graph.DELETE("/edit/relationships/:id", middlewares.AuthPermission("graph:edge:delete"), service.DeleteGraphRelationship)
	}

	// 数据库管理接口
	database := r.Group("/database")
	{
		database.POST("/query", service.ExecuteQuery)              // 执行SQL查询
		database.GET("/tables", service.GetTableList)              // 获取表列表
		database.GET("/table/structure", service.GetTableStructure) // 获取表结构
		database.GET("/stats", service.GetDatabaseStats)           // 获取数据库统计信息
	}

	return r
}
